//
//  ABNavigationBar.m
//  AmountBody
//
//  Created by Qiu Jishuai on 2018/1/2.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

#import "ABNavigationBar.h"
#import "ReactiveObjC.h"

static const CGFloat buttonW = 44;
static const CGFloat buttonH = 44;
static const CGFloat margin = 25;
static const CGFloat y = 20;

@interface ABNavigationBar()
@property(nonatomic, strong) UIViewController *currentViewController;
@property(nonatomic, strong) UINavigationController *currentNav;
@end

@implementation ABNavigationBar

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    self.backgroundColor = RGBColorAlpha(44, 44, 44, 1);
    if(CGRectEqualToRect(self.frame, CGRectZero)) {
        self.frame = CGRectMake(0, 0, SCREEN_W, TOPNAVIGATIONBAR_H);
    }
    self.useDefaultReturnBehavior = YES;
    self.hiddenLeftWhenInStackBottom = YES;
}

- (UIButton *)leftButton {
    if (!_leftButton) {
        _leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _leftButton.frame = CGRectMake(margin, y, buttonW, buttonH);
        [_leftButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [_leftButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateSelected];
        _leftButton.imageEdgeInsets = UIEdgeInsetsMake(2, 2, 2, 2);
        _leftButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_leftButton addTarget:self action:@selector(leftButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_leftButton];
    }
    return _leftButton;
}

- (void)didMoveToSuperview {
    [super didMoveToSuperview];

    UINavigationController *nav = [self currentNav];
    @weakify(self)
    if ([nav isKindOfClass:[UINavigationController class]]) {
        [nav setNavigationBarHidden:YES animated:NO];

        if (self.hiddenLeftWhenInStackBottom) { //在栈底时隐藏
            [RACObserve(nav, viewControllers) subscribeNext:^(NSArray *controllers) {
                @strongify(self)
                self.leftButton.hidden = [controllers.firstObject isEqual: self. currentViewController];
            }];
        }
    }
}

- (void)leftButtonClicked: (UIButton *)sender {
    if (self.leftButtonTaps) {
        self.leftButtonTaps();
    }
    //没有设置返回的时候  使用默认返回行为
    if (self.useDefaultReturnBehavior) {
        UINavigationController *nav = self.currentNav;
        if ([nav.visibleViewController isEqual: self.currentViewController]) {
            [nav popViewControllerAnimated:YES];
        }
    }
}

- (UIViewController *)currentViewController {
    if (!_currentViewController) {
        UIResponder *responder = (UIResponder *)self.nextResponder;
        while (![responder isKindOfClass:[UIViewController class]] && responder) {
            responder = responder.nextResponder;
        }
        _currentViewController = (UIViewController *)responder;
    }
    return _currentViewController;
}

- (UINavigationController *)currentNav {
    if (!_currentNav) {
        _currentNav = self.currentViewController.navigationController;
    }
    return _currentNav;
}

- (void)rightButtonClicked: (UIButton *)sender {
    if (self.rightButtonTaps) {
        self.rightButtonTaps();
    }
}

- (UIButton *)rightButton {
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _rightButton.frame = CGRectMake(SCREEN_W - buttonW - margin , 20, buttonW, buttonH);
        [_rightButton setTitle:@"提交" forState:UIControlStateNormal];
        [_rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_rightButton sizeToFit];
        [self addSubview:_rightButton];
        [_leftButton addTarget:self action:@selector(rightButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightButton;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 44)];
        _titleLabel.font = [UIFont systemFontOfSize:18];
        _titleLabel.textColor = [UIColor whiteColor];
        [_titleLabel sizeToFit];
        CGFloat width = _titleLabel.frame.size.width;
        _titleLabel.frame = CGRectMake((SCREEN_W - width)/2.0, 20, width, 44);
       
    }
    [self addSubview:_titleLabel];
    return _titleLabel;
}

@end
